﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Risesun.Extend
{
    /// <summary>
    /// 类型转换工具类
    /// </summary>
    public static partial class ConvertHelper
    {
        public static bool ToBool(this string str)
        {
            bool val = false;
            bool.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Int16类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static short ToShort(this string str)
        {
            short val = 0;
            short.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Int32类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static int ToInt(this string str)
        {
            int val = 0;
            int.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Decimal类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static decimal ToDecimal(this string str)
        {
            decimal val = 0M;
            decimal.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Int64类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static long ToLong(this string str)
        {
            long val = 0L;
            long.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Float类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static float ToFloat(this string str)
        {
            var val = 0F;
            float.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Double类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static double ToDouble(this string str)
        {
            var val = 0D;
            double.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成Guid类型
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static Guid ToGuid(this string str)
        {
            var val = Guid.Empty;
            Guid.TryParse(str, out val);
            return val;
        }

        /// <summary>
        /// String转换成DateTime
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string str)
        {
            var val = DateTime.MinValue;
            DateTime.TryParse(str, out val);
            return val;
        }
    }

    /// <summary>
    /// 类型转换工具类
    /// </summary>
    public static partial class ConvertHelper
    {
        /// <summary>
        /// String转换成bool列表
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static IEnumerable<bool> ToBoolList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToBool());
        }

        /// <summary>
        /// String转换成Int16类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<short> ToShortList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToShort());
        }

        /// <summary>
        /// String转换成Int32类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<int> ToIntList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToInt());
        }

        /// <summary>
        /// String转换成Decimal类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<decimal> ToDecimalList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToDecimal());
        }

        /// <summary>
        /// String转换成Int64类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<long> ToLongList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToLong());
        }

        /// <summary>
        /// String转换成Float类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<float> ToFloatList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToFloat());
        }

        /// <summary>
        /// String转换成Double类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<double> ToDoubleList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToDouble());
        }

        /// <summary>
        /// String转换成Guid类型列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<Guid> ToGuidList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToGuid());
        }

        /// <summary>
        /// String转换成DateTime列表
        /// </summary>
        /// <param name="str">String值</param>
        /// <returns></returns>
        public static IEnumerable<DateTime> ToDateTimeList(this string str)
        {
            var strs = str.Split(',');
            return strs.Select(s => s.Trim().ToDateTime());
        }
    }
}